package com.bjwl.web.intereptor;

import com.bjwl.pojo.Admin;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @Auther: com.itdai
 * @Date: 2019/10/14
 * @Description: com.itdai.web.interceptor
 * @Version: 1.0
 */

@Component
public class AdminLoginInterceptor implements HandlerInterceptor {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(AdminLoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
        log.info("进入Controller前");
        if(request.isRequestedSessionIdValid()){
            System.out.println(request.getRequestURI());
            Admin admin = (Admin)request.getSession().getAttribute("admin");
            if(admin == null){
                request.getRequestDispatcher("/mui/login").forward(request,response);
                return false;
            }else {
                return true;
            }
        }
        request.getRequestDispatcher("/mui/login").forward(request,response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        log.info("处理请求完成后视图渲染之前的处理操作");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.info("视图渲染之后的操作");
    }

}
